<?php
/*
 * @Author: 哲学家乔布斯 <wuyangii@qq.com>
 * @Date: 2022-10-20 21:41:37
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2022-11-07 22:37:15
 * @Descripttion: 用户信息
 */
namespace app\agent\controller\system;
use think\Db;
class BusForm extends \app\agent\controller\Common
{
    public function index(){
    
        return view();
    }

    public function list(){
        $limit = request()->param('limit') ?? 15;
        $page = request()->param('page');
        $page = $page ? ($page - 1) * $limit : 0;
        $search = request()->param();
        $query = (new \app\common\model\BusForm())->where(['agent_id' => $this->userInfo['id']]);
        isset($search['goods_title']) && $search['goods_title'] != "" ? $query->where("title like '%{$search['goods_title']}%'") : '';
        isset($search['id']) && $search['id'] != "" ? $query->where("id = '{$search['id']}'") : '';
        $query->order("id desc");

        $list = $query->limit($page, $limit)->append(['addtime_text'])->select();
        $count = $query->count();
        return self::successResult('',$list,$count);
    }
   
    public function is_state()
   {
       $id = request()->param('id');
       $state = request()->param('is_state');
       if (empty($id)) $this->error("请选择要修改的数据");
       $form = (new \app\common\model\BusForm())->where('id', $id)->find();
       $bus = (new \app\common\model\Bus())->where(['id' => $form['bus_id']])->find();
       $bus_new_money = aconfig("bus_new_money",$this->userInfo['id']);
       if($form['state'] != 0){
            return self::errorResult("已经处理完了");
       }
        Db::startTrans();
        try{
            $message = "订单创建失败,请重试";
            $process = true;
            $row = Db::table("{$this->tdb}bus_form")->where('id', $id)->update(['state' => $state]);
            if(!$row){
                $message = "修改团长状态失败,请重试";
                $process = false;
            }
            $row == Db::table("{$this->tdb}bus")->insert([
                'account' => $form['phone'],
                'nickname' => $form['nickname'],
                'avatar' => $form['avatar'],
                'agent_id' => $form['agent_id'],
                'phone' => $form['phone'],
                'address' => $form['address'],
                'user_id' => $form['id'],
                'latitude' => $form['latitude'],
                'longitude' => $form['longitude'],
                'addtime' => time(),
                'pid' => empty($form['bus_id']) ? 0 : $form['bus_id'],
                'state' => 0,
            ]);
            if(!$row){
                $message = "添加团长失败,请重试";
                $process = false;
            }
           
            if($form['bus_id'] != 0 && $bus_new_money != 0){
            #-------------------------------修改提货点资金日志-----------------------
                $row = Db::table("{$this->tdb}bus_money_log")->insert([
                    'title' => "拉新收益",
                    'bus_id' => $form['bus_id'],
                    'type' => 0,
                    'money' => $bus_new_money,
                    'addtime' => time(),
                    'order_id' => 0,
                    'agent_id' => $this->userInfo['id'],
                ]); 
                if(!$row){
                    $message = "修改资金日志失败,请重试";
                    $process = false;
                }
                $row = Db::table("{$this->tdb}bus")->where([
                    'id' => $form['bus_id'],
                    'money' => $bus['money']
                ])->update([
                    'money' => $bus['money']+$bus_new_money
                ]);
                if(!$row){
                    $message = "修改资金失败,请重试";
                    $process = false;
                }
            
            }
            if ($process !== false) {
                Db::commit(); // 提交事务
            } else {
                Db::rollback(); // 回滚事务
            }
         } catch (\Exception $e) {
            $process = false;
            $message = $e->getMessage();
            Db::rollback(); // 回滚事务
        }

        if ($process !== false) {
            return self::successResult("成功");
        } else {
            return self::errorResult($message);
        }
      
      
   }
}
